import type { Ref } from 'vue'

export const writer = (message: Ref, text: string) => {
  let counter: number = 0
  let intervalId = setInterval(function () {
    let displayText = text.substring(0, counter)
    message.value = '「' + displayText + '」'

    if (counter >= text.length) {
      clearInterval(intervalId)
      const Timeout = setTimeout(() => {
        intervalId = setInterval(() => {
          displayText = text.substring(0, counter)
          message.value = '「' + displayText + '」'
          if (counter <= 0) {
            clearInterval(intervalId)
            clearTimeout(Timeout)
          }
          counter--
        }, 40)
      }, 1500)
    }
    counter++
  }, 100)
}
